/*
 * VentanaPrincipal.java
 */

package desktopapplication3;

import backend.Language;
import backend.Languages;
import java.awt.Image;
import java.awt.Toolkit;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import java.util.Locale;
import java.util.Map;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * The application's main frame.
 */
public class VentanaPrincipal extends FrameView implements Constants {

    private int languageID;

    public VentanaPrincipal(SingleFrameApplication app) {
        
        super(app);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication3.DesktopApplication3.class).getContext().getResourceMap(VentanaPrincipal.class);

        initComponents();
         Map <String,Language> lan=null;
         jComboBox1.removeAllItems();
          try{
              lan = Languages.getList();
          } catch (Exception e){
              
                JOptionPane.showMessageDialog(null, resourceMap.getString( "errorLanguageMessage" ),"Error", JOptionPane.ERROR_MESSAGE);
          }
          int i=0;

          for(Language l: lan.values()){
                jComboBox1.insertItemAt(l.getName(), i++ );
          }
          jComboBox1.setSelectedIndex(0);


          
          this.languageID=jComboBox1.getSelectedIndex();
          this.getFrame().setTitle( resourceMap.getString("title") );

          Image photo= Toolkit.getDefaultToolkit().getImage( resourceMap.getString("img") );
          this.getFrame().setIconImage( photo );

          this.getFrame().setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

          this.getFrame().getRootPane().setDefaultButton(entrarButton);

          this.getFrame().setResizable(false);
          
      //El siguiente codigo setea la barra que ya no poseemos
      /*  // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });*/
    }

    @Action
    public void entrarButton() {
        languageID = jComboBox1.getSelectedIndex() + 1;

        switch( languageID ){
            case ENGLISH_CODE:
                Locale.setDefault( Locale.ENGLISH );
                break;
            case SPANISH_CODE:
                Locale.setDefault( new Locale("es","AR") );
                break;
        }

        SigninFrame view = new SigninFrame( languageID,this.getFrame() );
        view.setVisible(true);
        view.setLocationRelativeTo(null);
        this.getFrame().setVisible(false);


    }
    
    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = DesktopApplication3.getApplication().getMainFrame();
            aboutBox = new DesktopApplication3AboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        DesktopApplication3.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        mensajeEspañol = new javax.swing.JLabel();
        mensajeIngles = new javax.swing.JLabel();
        entrarButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication3.DesktopApplication3.class).getContext().getResourceMap(VentanaPrincipal.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        mainPanel.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 40, -1, 130));

        mensajeEspañol.setForeground(resourceMap.getColor("mensajeEspañol.foreground")); // NOI18N
        mensajeEspañol.setText(resourceMap.getString("mensajeEspañol.text")); // NOI18N
        mensajeEspañol.setName("mensajeEspañol"); // NOI18N
        mainPanel.add(mensajeEspañol, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 220, -1, -1));

        mensajeIngles.setForeground(resourceMap.getColor("mensajeEspañol.foreground")); // NOI18N
        mensajeIngles.setText(resourceMap.getString("mensajeIngles.text")); // NOI18N
        mensajeIngles.setName("mensajeIngles"); // NOI18N
        mainPanel.add(mensajeIngles, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 240, -1, -1));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(desktopapplication3.DesktopApplication3.class).getContext().getActionMap(VentanaPrincipal.class, this);
        entrarButton.setAction(actionMap.get("entrarButton")); // NOI18N
        entrarButton.setBackground(resourceMap.getColor("entrarButton.background")); // NOI18N
        entrarButton.setFont(resourceMap.getFont("entrarButton.font")); // NOI18N
        entrarButton.setForeground(resourceMap.getColor("entrarButton.foreground")); // NOI18N
        entrarButton.setMnemonic('e');
        entrarButton.setText(resourceMap.getString("entrarButton.text")); // NOI18N
        entrarButton.setName("entrarButton"); // NOI18N
        mainPanel.add(entrarButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 230, -1, 20));

        jLabel2.setName("jLabel2"); // NOI18N
        mainPanel.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        mainPanel.add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 230, 100, -1));

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setBorder(new javax.swing.border.LineBorder(resourceMap.getColor("jLabel4.border.lineColor"), 1, true)); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        mainPanel.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 560, 260));

        jLabel3.setIcon(resourceMap.getIcon("jLabel3.icon")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        jLabel3.setOpaque(true);
        mainPanel.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 10, 350, 260));

        setComponent(mainPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        languageID = jComboBox1.getSelectedIndex() + 1;

        switch( languageID ){
            case ENGLISH_CODE:
                entrarButton.setText("Enter");
                break;
            case SPANISH_CODE:
                entrarButton.setText("Entrar");
                break;
        }


    }//GEN-LAST:event_jComboBox1ActionPerformed



    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton entrarButton;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JLabel mensajeEspañol;
    private javax.swing.JLabel mensajeIngles;
    // End of variables declaration//GEN-END:variables

  /*  private final Timer messageTimer=null;
    private final Timer busyIconTimer=null;
    private final Icon idleIcon=null;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;*/

    private JDialog aboutBox;
}
